package com.bigstar.lolkor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Masteries_Defense extends Activity implements OnClickListener{
	public static final String[] title = {
		"Summoner's Resolve(소환사의 결의)", "Resistance(저항력)", "Hardiness(강건함)", "Tough Skin(질긴 피부)",
		"Durability(내구력)", "Vigor(원기)",
		"Indomitable(불굴의 의지)", "Veteran's Scars(고참병의 흉터)", "Evasion(회피)","Bladed Armor(칼날 갑옷)", 
		"Siege Commander(공성 사령관)","Initiator(선봉장)", "Enlightenment(깨우침)",
		"Honor Guard(의장대)", "Mercenary(용병)",
		"Juggernaut(파괴전차)"
	};
	public static final String[] text = {
		"다음 소환사 주문의 성능이 향상됩니다:\r\n부활 : 부활 시 짧은 시간 동안 이동 속도가 큰 폭으로 증가합니다.\r\n정화 : 방해 감소 효과 지속시간이 1초 증가합니다.\r\n치유 : 체력 회복량이 10% 증가합니다.\r\n강타 : 사용 시 10골드를 추가로 얻습니다.\r\n주둔 : 점령한 아군 포탑이 50%의 광역 피해를 줍니다.",
		"마법 저항력 +num",
		"방어력 +num",
		"미니언과 몬스터로부터 받는 피해가 num 감소합니다.",
		"레벨당 체력 +num\r\n(챔피언 18 레벨 달성시 체력 +27/54/81/106)",
		"5초당 체력 재생 +num",
		"받는 피해가 num 감소합니다.",
		"체력 +num",
		"광역 피해를 주는 스킬로부터 받는 피해가 num% 감소합니다.",
		"미니언과 몬스터로부터 공격받으면 num의 피해를 반사합니다.",
		"인접한 적 포탑의 방어력을 num 감소시킵니다.\r\n(중첩되지 않음)",
		"체력이 70% 이상일 때 이동 속도가 num% 증가합니다.",
		"레벨당 재사용 대기시간 감소 +num%\r\n(챔피언 18레벨 달성시 재사용 대기시간 감소 +2.7/5.4/8.1%)",
		"받는 피해가 num% 감소합니다.",
		"챔피언 킬, 어시스트시 num골드를 얻습니다.\r\n(수정의 상처에서는 효과 50% 감소)",
		"최대 체력이 3% 증가하며 방해 효과 지속시간이 num% 감소합니다."
	};
	public static final int max_point[] = { 1,3,3,2,4,3,2,1,3,1,1,3,3,3,3,1 };
	public static final String[][] level = {
		{"0"},
		{"2","4","6"},
		{"2","4","6"},
		{"1","2"},
		{"1.5","3","4.5","6"},
		{"1","2","3"},
		{"1","2"},
		{"30"},
		{"1","2","3"},
		{"6"},
		{"10"},
		{"1","2","3"},
		{"0.15","0.3","0.45"},
		{"0.5","1","1.5"},
		{"8","16","24"},
		{"10"}
	};
	public int spellid = 0;
	public static Activity context;
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
		setContentView(R.layout.masteries_defense); 
		context = this;
 		if(getIntent().getExtras() != null){
 			ImageView iv = null;
 			for(int i = 0; i < 16; i++){
 				iv = (ImageView)findViewById(R.id.d_00+i);
 				iv.setOnClickListener(this);
 			}
 			TextView tv = (TextView)findViewById(R.id.b_defense);
			tv.setText("방어(DEFENSE):  "+Builder.getMasteriesPoint(1));
			imageChange();
 		}else{
 			ImageView iv = null;
 			for(int i = 0; i < 16; i++){
 				iv = (ImageView)findViewById(R.id.d_00+i);
 				iv.setOnClickListener(this);
 			}
 		}
	}
	

	
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
 		if(getIntent().getExtras() != null){
 			ImageView iv = null;
 			for(int i = 0; i < 16; i++){
 				iv = (ImageView)findViewById(R.id.d_00+i);
 				iv.setOnClickListener(this);
 			}
 			TextView tv = (TextView)findViewById(R.id.b_defense);
			tv.setText("방어(DEFENSE):  "+Builder.getMasteriesPoint(1));
			imageChange();
 		}
    }
	
	public static void imageChange(){
		ImageView iv = null;
		Bitmap bitmap = null;
		TextView tv = null;
		tv = (TextView)context.findViewById(R.id.b_defense);
		tv.setText("방어(DEFENSE):  "+Builder.getMasteriesPoint(1));
		for(int i = 0; i < 16; i++){
			tv = (TextView)context.findViewById(R.id.d_text_00+i);
			tv.setText(" "+Builder.masteries[1][i]+"/"+max_point[i]);
			switch(i){
			case 4: 
			case 5:
				if(Builder.getMasteriesPoint(1) < 4){
					iv = (ImageView)context.findViewById(R.id.d_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.d_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 6:
			case 8:
				if(Builder.getMasteriesPoint(1) < 8){
					iv = (ImageView)context.findViewById(R.id.d_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.d_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 7: 
				if(Builder.masteries[1][4] < 4){
					iv = (ImageView)context.findViewById(R.id.d_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.d_00+i);
					iv.setImageBitmap(bitmap);
					
					iv = (ImageView)context.findViewById(R.id.m_p_00);
					iv.clearColorFilter();
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
					iv.setImageBitmap(bitmap);
					continue;
				}
				if(Builder.getMasteriesPoint(1) < 8){
					iv = (ImageView)context.findViewById(R.id.d_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.d_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				iv = (ImageView)context.findViewById(R.id.m_p_00);
				iv.setColorFilter(Color.GREEN, PorterDuff.Mode.DARKEN);
				bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
				iv.setImageBitmap(bitmap);
				break;
			case 9: 
				if(Builder.masteries[1][3] < 2){
					iv = (ImageView)context.findViewById(R.id.d_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.d_00+i);
					iv.setImageBitmap(bitmap);
					
					iv = (ImageView)context.findViewById(R.id.m_p_01);
					iv.clearColorFilter();
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_1);
					iv.setImageBitmap(bitmap);
					continue;
				}
				if(Builder.getMasteriesPoint(1) < 8){
					iv = (ImageView)context.findViewById(R.id.d_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.d_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				
				iv = (ImageView)context.findViewById(R.id.m_p_01);
				iv.setColorFilter(Color.GREEN, PorterDuff.Mode.DARKEN);
				bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_1);
				iv.setImageBitmap(bitmap);
				break;
			case 10:
			case 11:
			case 12:
				if(Builder.getMasteriesPoint(1) < 12){
					iv = (ImageView)context.findViewById(R.id.d_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.d_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 13:
			case 14:
				if(Builder.getMasteriesPoint(1) < 16){
					iv = (ImageView)context.findViewById(R.id.d_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.d_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 15:
				if(Builder.getMasteriesPoint(1) < 20){
					iv = (ImageView)context.findViewById(R.id.d_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.d_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			}
			iv = (ImageView)context.findViewById(R.id.d_00+i);
			iv.clearColorFilter();
			bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.d_00+i);
			iv.setImageBitmap(bitmap);
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.b_champ_m_minus:
			if(Builder.masteries[1][spellid] > 0){
				Builder.masteries[1][spellid]--;
				set();
			}
			break;
		case R.id.b_champ_m_plus:
			if(Builder.getMasteriesCount() >= 30){
				Toast.makeText(this, "포인트 30 다씀", Toast.LENGTH_SHORT).show();
				return;
			}
			switch(spellid){
			case 4: 
			case 5:
				if(Builder.getMasteriesPoint(1) < 4){
					return;
				}
				break;
			case 6:
			case 8: 
				if(Builder.getMasteriesPoint(1) < 8){
					return;
				}
				break;
			case 7: 
				if(Builder.masteries[1][4] < 4){
					return;
				}
				if(Builder.getMasteriesPoint(1) < 8){
					return;
				}
				break;
			case 9: 
				if(Builder.masteries[1][3] < 2){
					return;
				}
				if(Builder.getMasteriesPoint(1) < 8){
					return;
				}
				break;
			case 10:
			case 11:
			case 12:
				if(Builder.getMasteriesPoint(1) < 12){
					return;
				}
				break;
			case 13:
			case 14:
				if(Builder.getMasteriesPoint(1) < 16){
					return;
				}
				break;
			case 15:
				if(Builder.getMasteriesPoint(1) < 20){
					return;
				}
				break;
			}
			if(Builder.masteries[1][spellid] < max_point[spellid]){
				Builder.masteries[1][spellid]++;
				set();
			}
			break;
		default:
			dialog(v.getId() - R.id.d_00);
			break;
		}
	}
	
	public void set(){
		TextView tv = (TextView)findViewById(R.id.b_defense);
		tv.setText("방어(DEFENSE):  "+Builder.getMasteriesPoint(1));
		tv = (TextView)findViewById(R.id.d_text_00+spellid);
		tv.setText(" "+Builder.masteries[1][spellid]+"/"+max_point[spellid]);
		tv = (TextView) layout.findViewById(R.id.b_champ_m_text);
		tv.setText("단계: "+Builder.masteries[1][spellid]+"/"+max_point[spellid]+"\r\n\r\n"+text[spellid]);
		String limit = "";
		switch(spellid){
		case 4: 
		case 5: limit = "\r\nDefense 4포인트 필요\r\n"; break;
		case 6: 
		case 8: limit = "\r\nDefense 8포인트 필요\r\n"; break;
		case 7: limit = "\r\nDefense 8포인트 필요\r\nDurability 4포인트 필요\r\n"; break;
		case 9: limit = "\r\nDefense 8포인트 필요\r\nTough Skin 2포인트 필요\r\n"; break;
		case 10: 
		case 11:
		case 12: limit = "\r\nDefense 12포인트 필요\r\n"; break;
		case 13:
		case 14: limit = "\r\nDefense 16포인트 필요\r\n"; break;
		case 15: limit = "\r\nDefense 20포인트 필요\r\n"; break;
		}
		if(Builder.masteries[1][spellid] >= max_point[spellid]){
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[1][spellid]-1]);
			tv.setText("단계: "+Builder.masteries[1][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t);	
		}else if(Builder.masteries[1][spellid] == 0){
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[1][spellid]]);
			tv.setText("단계: "+Builder.masteries[1][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t);	
		}else{
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[1][spellid]-1]);
			String next = text[spellid];
			next = next.replaceAll("num", level[spellid][Builder.masteries[1][spellid]]);
			tv.setText("단계: "+Builder.masteries[1][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t
			          +"\r\n\r\n다음 단계:\r\n"+next);	
		}
	}
	
	View layout;
	
	private void dialog(int id) {
 		if(getIntent().getExtras() != null){
 			spellid = id;
 			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
 			layout = inflater.inflate(R.layout.build_masteries,(ViewGroup) findViewById(R.id.b_champ_m));
 			set();
 			Button bt = (Button)layout.findViewById(R.id.b_champ_m_minus);
 			bt.setOnClickListener(this);
 			bt = (Button)layout.findViewById(R.id.b_champ_m_plus);
 			bt.setOnClickListener(this);
 			AlertDialog.Builder alt_bld = new AlertDialog.Builder(getParent()); 
 			alt_bld
 			.setView(layout)
 			.setCancelable(false).setNegativeButton("닫기",  
 					new DialogInterface.OnClickListener() {
 				public void onClick(DialogInterface dialog, int id) {
 					imageChange();
 					dialog.cancel();
 				}});  
 			AlertDialog alert = alt_bld.create();  
 			alert.setTitle(title[id]);
 			alert.setIcon(R.drawable.d_00+id);
 			alert.show(); 
 		}else{
			String num = "";
			String t = text[id];
			for(int i = 0; i < level[id].length; i++){
				num = num+level[id][i];
				if(level[id].length == 1){
				}else if(i+1 < level[id].length){
					num = num+"/";
				}
			}
			t = t.replaceAll("num", num);
			TextView tv = new TextView(this);
			tv.setText(t);
			tv.setTextSize(14);
 			AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
 			alt_bld.setView(tv).setNegativeButton("닫기",  
 					new DialogInterface.OnClickListener() {
 				public void onClick(DialogInterface dialog, int id) {
 					dialog.cancel();
 				}});  
 			AlertDialog alert = alt_bld.create();  
 				alert.setTitle(title[id]);
 				alert.setIcon(R.drawable.d_00+id);
 				alert.show();	
 		} 
	}

}